package com.dongdong.log;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAspect {
    private Logger logger = LoggerFactory.getLogger(LogAspect.class);
    //针对个别方法也需要打印日志时  通过配置注解的形式来对该宝下的方法 进行日志打印
    @Pointcut("execution(public * com.dongdong.service.*.*(..)) || @annotation(com.dongdong.log.Dklog)")
    public void logAop(){

    }
    @Before("logAop()")
    public void doBefore(JoinPoint joinPoint){
        //返回object类型的原因：参数类型不确定，个数不确定
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        logger.info("进入方法: {},参数为: {}",signature,args);
    }
    @AfterReturning(pointcut = "logAop()",returning = "obj")
    public void doAfterReturning(Object obj) throws Throwable{
        logger.info("方法执行成功 返回值为: {}", JSONObject.toJSONString(obj));
    }


}
